
//
//  MDBaseRoomController.swift
//  麦兜直播
//
//  Created by yeqingyu on 16/10/22.
//  Copyright © 2016年 yeqingyu. All rights reserved.
//

import UIKit
private let margin:CGFloat = 10.0
private let seactionHeadH:CGFloat = 40
private let itemW = (screenWidth - 3*margin)*0.5

private let NORMALID = "MDNormalCellID"
private let TITLEBARID = "MDTitleBarViewID"

class MDBaseRoomController: MDAnimationController {
     var baseNetTool:MDBaseNetTool!
     lazy var collectionView:UICollectionView = {[unowned self] in
        let layout = UICollectionViewFlowLayout()
        let itemH = itemW*3/4
        layout.itemSize = CGSize(width:itemW , height: itemH)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.sectionInset = UIEdgeInsets(top: 0, left: margin, bottom: 0, right: margin)
        layout.headerReferenceSize = CGSize(width: screenWidth, height: seactionHeadH)
        let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        collectionView.backgroundColor = UIColor.white
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(UINib(nibName: "MDNormalCell", bundle: nil), forCellWithReuseIdentifier: NORMALID)
        collectionView.register(UINib(nibName: "MDTitleBarView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: TITLEBARID)
        return collectionView
        }()

    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
        requestData()

    }
}
extension MDBaseRoomController{
      override func setUpUI(){
        contentView = collectionView
        super.setUpUI()
        view.addSubview(self.collectionView)
    }
    
    
      func requestData() {

   }
}
// MARK:- 数据源和代理方法
extension MDBaseRoomController:UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    
    func numberOfSections(in collectionView: UICollectionView) -> Int{
        
        return baseNetTool.baseRoomGroup.count
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let homeGroup = baseNetTool.baseRoomGroup[section]
        return homeGroup.rooms.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let homeGroup = baseNetTool.baseRoomGroup[indexPath.section]
        let home = homeGroup.rooms[indexPath.row]
        
        let normalCell = collectionView.dequeueReusableCell(withReuseIdentifier: NORMALID, for: indexPath) as!MDNormalCell
        normalCell.home = home
        return normalCell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let headView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: TITLEBARID, for: indexPath) as! MDTitleBarView
        let  homeGroup = baseNetTool.baseRoomGroup[indexPath.section]
        headView.homeGroup = homeGroup
        return headView
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        return CGSize(width: itemW, height: itemW*3/4)
    }
}

//代理方法

extension MDBaseRoomController:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let homeGroup = baseNetTool.baseRoomGroup[indexPath.section]
        let home = homeGroup.rooms[indexPath.row]
        
        home.isVertical==0 ? jumpToPcViewRoom() :jumpToPhoneViewRoom()
    }
    
    // MARK:- 跳转到手机直播页面
    private func jumpToPhoneViewRoom(){
        let phoneVc = MDPhoneController()
    
        present(phoneVc, animated: true, completion: nil)
    
    }
    
    // MARK:- 跳转到电脑直播页面
    private func jumpToPcViewRoom(){
    
        let pcVC = MDPCViewController()
        
        navigationController?.pushViewController(pcVC, animated: true)
        
    }
}
